<template>
  <div>
    <!--添加职位-->
    <div>
      <!--@keydown.enter.native="addPosition"键盘按下事件-->
      <el-input
          size="small"
          class="addPosInput"
          placeholder="添加职位"
          suffix-icon="el-icon-plus"
          @keydown.enter.native="addPosition"
          v-model="pos.name">
      </el-input>
      <el-button type="primary" icon="el-icon-plus" size="mini" @click="addPosition()">添加</el-button>
    </div>

    <!--表格-->
    <div class="posManaMain">
      <!-- @selection-change="handleSelectionChange">单选框选择改变事件-->
      <el-table
          @selection-change="handleSelectionChange"
          border
          stripe
          size="small"
          :data="positions"
          style="width: 70%">
        <el-table-column type="selection" width="55"/>
        <el-table-column
            prop="id"
            label="编号"
            width="55">
        </el-table-column>
        <el-table-column
            prop="name"
            label="职位"
            width="120">
        </el-table-column>
        <el-table-column
            prop="createdate"
            label="创建时间"
            width="200">
        </el-table-column>
        <!--        <el-table-column
                    prop="enabled"
                    label="是否启用"
                    width="120">
                </el-table-column>-->
        <el-table-column
            label="操作">
          <template slot-scope="scope">
            <!--row代表一行数据，index代表索引-->
            <el-button
                size="mini"
                @click="showEditView(scope.$index, scope.row)">编辑
            </el-button>
            <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.$index, scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--如果多选框有数据，就显示批量删除按钮-->
    <el-button type="danger" size="small" style="margin-top: 8px;"
               :disabled="this.multipleSelection==0" @click="deleteMany">批量删除
    </el-button>

    <!--编辑弹框-->
    <el-dialog
        title="提示"
        :visible.sync="showEditFlag"
        width="30%"
        :before-close="handleClose">
      <div>
        <el-tag>职位名称</el-tag>
        <el-input v-model="updatePos.name" size="small" class="updatePosInput"></el-input>
      </div>
      <span slot="footer" class="dialog-footer">
          <el-button size="small" @click="showEditFlag = false">取 消</el-button>
          <el-button size="small" type="primary" @click="doUpdate">确 定</el-button>
        </span>
    </el-dialog>

  </div>
</template>

<script>
export default {
  name: "PosMana",
  data() {
    return {
      pos: {
        name: '',
      },
      // 表格数据
      positions: [],
      // 表格弹框
      showEditFlag: false,

      // 编辑更新数据
      updatePos: {
        name: '',
      },
      // 选择的数组
      multipleSelection: [],
    }
  },
  // 页面初始化调用初始化表格数据方法
  mounted() {
    this.initPosition();
  },
  methods: {
    // 初始化职位表格数据
    initPosition() {
      this.getRequest('/system/basic/pos/')
          .then(result => {
            if (result) {
              this.positions = result;
            }
          })
    },
    // 添加职位
    addPosition() {
      if (this.pos.name == '') {
        this.$message.error("职位名称不能为空");
      } else {
        this.postRequest('/system/basic/pos/', this.pos)
            .then(result => {
              if (result) {
                // 添加成功重新加载页面
                this.initPosition();
                // 清空输入框
                this.pos.name = '';
              }
            })
      }
    },
    // 回显职位信息
    handleClose() {
      this.showEditFlag = false;
    },
    showEditView(index, data) {
      // 回显数据
      // 使用拷贝代替直接赋值，解决双向赋值的问题
      Object.assign(this.updatePos, data);
      // 将前端传入的日期设置为空
      this.updatePos.createdate = '';
      this.showEditFlag = true;
    },
    // 更新数据
    doUpdate() {
      this.putRequest('/system/basic/pos/', this.updatePos)
          .then(result => {
            console.log(result);
            if (result) {
              this.initPosition();
              this.showEditFlag = false;
            }
          })
    },

    // 根据id删除一条职位信息
    handleDelete(index, data) {
      this.$confirm('此操作将永久删除[' + data.name + ']职位, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteRequest('/system/basic/pos/' + data.id)
            .then(result => {
              if (result) {
                // 重新加载页面
                this.initPosition();
              }
            })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },

    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 批量删除
    deleteMany() {
      this.$confirm('此操作将永久删除' + this.multipleSelection.length + '条职位, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let ids = '?';
        this.multipleSelection.forEach(item => {
          // 拼接成id字符串数组
          ids += 'ids=' + item.id + '&';
        })
        this.deleteRequest('/system/basic/pos/' + ids)
            .then(result => {
              if (result) {
                // 重新加载页面
                this.initPosition();
              }
            })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
  }
}
</script>

<style>
.addPosInput {
  width: 300px;
  margin-right: 8px;
}

.posManaMain {
  margin-top: 10px;
}

.updatePosInput {
  width: 200px;
  margin-left: 8px;
}
</style>